package com.weixin.sendmsg.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @ClassName CaiHongPi
 * @Description TODO
 *
 */
public class CaiHongPi {
    private static String key = "xxx";
    private static String url = "http://api.tianapi.com/caihongpi/index?key=";

    /**
     * 调用彩虹屁的API文档
     */
   private static String URL = "https://api.shadiao.pro/chp";

    private static List<String> jinJuList = new ArrayList<>();
    private static String name = "老婆";


    public static String getCaiHongPi() {
        //默认彩虹屁
        String str = "阳光落在屋里，爱你藏在心里";
        try {
            JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getUrl(URL));
            str = jsonObject.getJSONObject("data").getString("text");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 载入金句库
     * 读取每日一言文件
     * 已经替换成API接口获取金句
     */
    static {
        InputStream inputStream = CaiHongPi.class.getClassLoader().getResourceAsStream("jinju.txt"); //读取本地配置文件
        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
            String str = "";
            String temp = "";
            while ((temp = br.readLine()) != null) {
                if (!StringUtils.isEmpty(temp)) {
                    str = str + "\r\n" + temp;
                } else {
                    jinJuList.add(str);
                    str = "";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 随机返回一条金句
     * @return
     */
    public static String getJinJu() {
//        Random random = new Random(); //读取本地的数据时需要
//        return jinJuList.get(random.nextInt(jinJuList.size())); //读取本地的数据时需要

        String str = "";
        try {
            JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getUrl("https://saying.api.azwcl.com/saying/get"));
             str = (String) jsonObject.getJSONObject("data").get("content");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return str;
    }

    public static void main(String[] args) {
        System.out.println(getJinJu());
    }
}
